package com.hlz.fastnote

import android.graphics.Color
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

object ToastUtil {

    private var toast: Toast? = null
    private var textView: TextView? = null
    private val context by lazy { App.app }

    fun showToast(content: Any?) {
        try {
            if (content == null || content.equals("")) return
            if (toast == null) {
                toast = Toast.makeText(context, content.toString(), Toast.LENGTH_SHORT)
                textView = TextView(context)
                textView?.text = content.toString()
                textView?.setTextColor(Color.parseColor("#ffffff"))
                textView?.textSize = 14f
                val layout = LinearLayout(context)
                layout.gravity = Gravity.CENTER
                layout.addView(textView)
                layout.background = context.resources.getDrawable(R.drawable.toast_bg)
                layout.setPadding(
                    DimenUtils.dip2px(context, 16),
                    DimenUtils.dip2px(context, 8),
                    DimenUtils.dip2px(context, 16),
                    DimenUtils.dip2px(context, 8)
                )
                toast?.view = layout
                toast?.setGravity(Gravity.CENTER, 0, 0)
            } else {
                textView?.text = content.toString()
            }
            toast?.show()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}